package com.jyy.prefabricated.service.impl;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.AccessoryFile;
import com.jyy.prefabricated.entity.Content;
import com.jyy.prefabricated.enums.DeletedEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.AccessoryFileMapper;
import com.jyy.prefabricated.mapper.ContentMapper;
import com.jyy.prefabricated.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 内容表(Content)表服务实现类
 *
 * @author makejava
 * @since 2021-05-13 14:43:01
 */
@Service("contentService")
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentDao;

    @Autowired
    private AccessoryFileMapper fileMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Content queryById(Integer id) {
        AccessoryFile accessoryFile = new AccessoryFile();
        accessoryFile.setPid(id);

        List<AccessoryFile> files = fileMapper.queryAll(accessoryFile);
        Content content = contentDao.queryById(id);
        content.setFiles(files);
        return content;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Content> queryAllByLimit(int offset, int limit) {
        return this.contentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param content 实例对象
     * @return 实例对象
     */
    @Override
    public Content insert(Content content) {
        content.setDeleted(DeletedEnum.INVALID.getValue());
        int id = contentDao.insert(content);

        saveCourseId2File(content.getFiles(), content.getId());
        return content;
    }

    public void saveCourseId2File(List<AccessoryFile> files, Integer id) {
        if (files != null && files.size() > 0) {
            for (AccessoryFile file : files) {
                file.setPid(id);
            }
            fileMapper.updateBatch(files);
        }
    }

    /**
     * 修改数据
     *
     * @param content 实例对象
     * @return 实例对象
     */
    @Override
    public Content update(Content content) {
        if (content.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        saveCourseId2File(content.getFiles(), content.getId());
        contentDao.update(content);
        return queryById(content.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.contentDao.deleteById(id) > 0;
    }

    @Override
    public List<Content> queryList(String contentName, Integer contentCategoryId, Integer studentServiceId, Integer deleted , String orderStr) {
        Content content = new Content();
        content.setContentName(contentName);
        content.setContentCategoryId(contentCategoryId);
        content.setStudentServiceId(studentServiceId);
        content.setDeleted(DeletedEnum.INVALID.getValue());
        content.setOrderStr(orderStr);
        return this.contentDao.queryAll(content);
    }

    @Override
    public int batchUpdateDeleted(BatchDto dto) {
        return contentDao.batchUpdateDeleted(dto);
    }
}
